using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace MimeCloud.Business.Helpers
{
    public class IOHelper
    {
        /// <summary>
        /// Copys a directory (recursivly) to a sepcified destination
        /// </summary>
        /// <param name="sourcePath">Source directory to copy</param>
        /// <param name="destinationPath">Destination directory to copy to</param>
        public static void CopyDirectory(string sourcePath, string destinationPath)
        {
            DirectoryInfo source = new DirectoryInfo(sourcePath);
            DirectoryInfo destination = new DirectoryInfo(destinationPath);

            if (!destination.Exists)
                destination.Create();

            foreach (FileInfo file in source.GetFiles())
            {
                //Ignore source safe files
                if (file.Extension.ToLower() != ".scc")
                    file.CopyTo(Path.Combine(destination.FullName, file.Name));
            }

            foreach (DirectoryInfo subDirectory in source.GetDirectories())
                CopyDirectory(subDirectory.FullName, Path.Combine(destination.FullName, subDirectory.Name));
        }

        /// <summary>
        /// Ensures that no file in a specified directory (Recursive) has a read-only flag
        /// </summary>
        public static void RemoveReadOnly(string directoryPath)
        {
            foreach (string s in Directory.GetFiles(directoryPath))
            {
                FileInfo i = new FileInfo(s);
                i.IsReadOnly = false;
            }

            foreach (string s in Directory.GetDirectories(directoryPath))
                RemoveReadOnly(s);
        }



        /// <summary>
        /// Clears the contents of the application's temp folder
        /// </summary>
        public static void ClearTempDirectory(DirectoryInfo parentDirectory, string defaultTempLocation)
        {
            //Set parent to the root temp dir if it is null
            if (parentDirectory == null)
                parentDirectory = new DirectoryInfo(defaultTempLocation);

            //Clear files, ensuring they aren't read only
            foreach (FileInfo i in parentDirectory.GetFiles())
            {
                i.IsReadOnly = false;
                i.Delete();
            }

            //Clear directories, ensuring they are empty first
            foreach (DirectoryInfo i in parentDirectory.GetDirectories())
            {
                ClearTempDirectory(i,null);
                i.Delete(true);
            }
        }

        /// <summary>
        /// Checks that the given directory name does not contain any
        /// of the specified masks
        /// </summary>
        public static bool AreFolderMasksNotPresent(string text, string[] masks)
        {
            bool flag = false;
            Regex rx;

            foreach (string m in masks)
            {
                rx = new Regex(m, RegexOptions.IgnoreCase);
                if (rx.IsMatch(text))
                    flag = true;
            }
            return (!flag);
        }

        /// <summary>
        /// Checks that the given file name has one of the given
        /// file extensions
        /// </summary>
        public static bool CompareFileMasks(string text, string[] includeFileExtensions, string[] excludeFileExtensions)
        {
            text = text.ToLower();

            //Exclude config files
            foreach (string s in excludeFileExtensions)
            {
                if (text.ToLower().EndsWith(s.ToLower()))
                    return false;
            }

            //Include requested file masks
            foreach (string m in includeFileExtensions)
            {
                if (text.ToLower().EndsWith(m.ToLower()))
                    return true;
            }
            return false;
        }

        /// <summary>
        /// Gets all top level directories for a specified path excluding ones that match the specified file masks
        /// </summary>
        /// <param name="parentPath">Path to get the children of</param>
        /// <param name="masks">Folder masks to match and remove</param>
        public static List<DirectoryInfo> GetAllowedDirectories(DirectoryInfo parentPath, string[] masks)
        {
            List<DirectoryInfo> directories = new List<DirectoryInfo>();
            foreach (DirectoryInfo info in parentPath.GetDirectories("*", SearchOption.TopDirectoryOnly))
            {
                //if (Path.GetFileName(info.FullName).ToLower() != AppCommon.Constants.BinDirectoryName.ToLower())
                //{
                    if (AreFolderMasksNotPresent(info.Name, masks))
                    {
                        directories.Add(info);
                        directories.AddRange(GetAllowedDirectories(info, masks));
                    }
                //}
            }

            return directories;
        }
    }
}
